﻿using System;
using System.Web.Mvc;
using eCRAM.Bll;
using eCRAM.Dao;
using eCRAM.Model;
using eCRAM.WebSite.Attributes;
using eCRAM.WebSite.Models;

namespace eCRAM.WebSite.Controllers
{
    [CustomAuthorization(UserRole.Administrator)]
    public class ExportController : Controller
    {
        public ActionResult ExportCrams()
        {
            return View("ExportCrams", new ExportModel{Year = DateTime.Today.Year, Month = DateTime.Today.Month});
        }

        [HttpPost]
        public ActionResult ProcessCramExport(ExportModel exportModel)
        {
            var errors = "";
            var nbCrams = 0;
            var listCrams = CramRepository.Search(exportModel.Year, exportModel.Month, null, null, null);
            foreach (var cram in listCrams)
            {
                if(cram.Status == CramStatus.New )
                {
                    errors += string.Format("Le cram de {0} est encore au statut 'Nouveau'.<br/>", cram.User);
                    continue;
                }

                new CramExcelFileGenerator(cram).Generate();
                new AstreinteFileGenerator(cram).Generate();

                nbCrams++;
            }
            exportModel.Resultat = string.Format("{0} Crams exportés.<br/>{1}", nbCrams, errors);
            return View("ExportCrams", exportModel);
            
        }

    }
}
